$(() => {
    const token = GetQueryString("token");
    let page = 1;
    let page_size = 10;
    ajaxList();

    // 收藏商品列表
    function ajaxList(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|company_favorite_list', 'p': page, 'page_size': page_size, "token": token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        html += `
                            <div class="collectWrap">
                                <div class="collectList" data-type="0">`
                        if (item.company_level >= 1) {
                            html += `<a href="shop_details_company.html?company_id=${item.company_id}&token=${token}" class="collectItem">`
                        } else {
                            html += `<a href="shop_details.html?company_id=${item.company_id}&token=${token}" class="collectItem">`
                        }
                        html += `<img src="${item.company_logo == null ? '../img/user.jpg' : item.company_logo}" alt="头像" class="collectHead">
                                        <div class="collecText">
                                            <p class="ringtct1">${item.company_name}</p>`
                        if (item.company_level >= 1) {
                            html += `<img src="${item.level_logo}" class="userLevel" />`
                        }
                        html += `</div>
                                    </a>
                                    <img class="collect3" src="../img/more.png" onClick="onOpen(this);" alt="更多">
                                </div>
                                <a href="javascript:;" class="del-btn" data-id="${item.company_id}" onclick="clickon(this)">取消关注</a>
                            </div>
                        `;
                    })
                    if (data.length < page_size) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.selectCompanyLists').append(html);
                    
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    //下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                ajaxList(page);
            }
        }
    })
})

// 点击滑动显示取消关注
function onOpen(_that) {
    let _this = _that;
    let type = $(_this).parent().attr('data-type');
    if (type == 0) {
        $(_this).parent().attr('data-type', 1);
        $(_this).parent().parent().siblings().children().removeClass('on');
        $(_this).parent().parent().siblings().children().attr('data-type', 0);
        $(_this).parent().addClass('on');
    } else {
        $(_this).parent().attr('data-type', 0);
        $(_this).parent().removeClass('on');
    }
}

// 取消关注
function clickon(e) {
    let _this = e;
    let company_id = $(_this).attr("data-id");
    const token = GetQueryString("token");
    // console.log(company_id);
    // console.log(token);
    $.ajax({
        type: "post",
        url: url,
        data: { 'method': 'User|company_favorite_del', 'company_id': company_id, "token": token },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            if (res.status == 1) {
                alert(res.message);
                $(_this).parent().remove();
            } else if (res.status == 2) {
                location.href = "fox://logout";
            } else {
                alert(res.message);
            }
        }
    })
}